﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject_SEM3_SecurityServices.ObjectClass;
using eProject_SEM3_SecurityServices.Bussiness;

namespace eProject_SEM3_SecurityServices
{
    public partial class EmployeeManager : System.Web.UI.Page
    {
        OEmployee employee;
        BEmployee employeeB;

        protected void Page_Load(object sender, EventArgs e)
        {
            employee = new OEmployee();
            employeeB = new BEmployee();
            if(!IsPostBack){
                BindDatagrv1();
            }

        }
        public void BindDatagrv1()
        {
            List<OEmployee> empList = new List<OEmployee>();
            empList = employeeB.GetEmployeeList();
            GridView1.DataSource = empList;
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<OEmployee> empList = new List<OEmployee>();
            empList = employeeB.GetEmployeeList();
            List<OEmployee> empList1 = new List<OEmployee>();

            switch (ddOption.SelectedValue)
            {
                case "1":

                    for (int i = 0; i < empList.Count; i++)
                    {
                        if (empList[i].EmployeeID == Convert.ToInt32(tbSearchContent.Text))
                        {
                            empList1.Add(empList[i]);
                        }
                    }

                    break;
                case "2":

                    for (int i = 0; i < empList.Count; i++)
                    {
                        if (empList[i].EmployeeFullName.Equals(tbSearchContent.Text))
                        {
                            empList1.Add(empList[i]);
                        }
                    }
                    break;
                case "3":

                    for (int i = 0; i < empList.Count; i++)
                    {
                        if (empList[i].EmployeeID == Convert.ToInt32(tbSearchContent.Text))
                        {
                            empList1.Add(empList[i]);
                        }
                    }
                    break;
                case "4":

                    for (int i = 0; i < empList.Count; i++)
                    {
                        if (empList[i].EmployeeID == Convert.ToInt32(tbSearchContent.Text))
                        {
                            empList1.Add(empList[i]);
                        }
                    }
                    break;
                case "5":

                    for (int i = 0; i < empList.Count; i++)
                    {
                        if (empList[i].EmployeeID == Convert.ToInt32(tbSearchContent.Text))
                        {
                            empList1.Add(empList[i]);
                        }
                    }
                    break;
            }

            GridView1.DataSource = empList1;
            GridView1.DataBind();

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbSearchContent.Text = ((Label)GridView1.SelectedRow.FindControl("Label1")).Text;
        }

        protected void btCreateNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmAddEmployee.aspx");
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmUpdateEmployee.aspx?Id=" + ((Label)GridView1.SelectedRow.FindControl("Label1")).Text);
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            try
            {
                BEmployee bemp = new BEmployee();
                bemp.DeleteEmployee(Convert.ToInt32(((Label)GridView1.SelectedRow.FindControl("Label1")).Text));
                Response.Write("<script>alert('Successful');</script>");
                BindDatagrv1();

            }catch(Exception ev){
                Response.Write("<script>alert('Not Successful');</script>");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            List<OEmployee> empList = new List<OEmployee>();
            empList = employeeB.GetEmployeeList();
            GridView1.DataSource = empList;
            GridView1.DataBind();
        }
    }
}